
#include "five.h"
#include "ui_five.h"
#include<QPainter>
#include<QMouseEvent>
#include<QPushButton>

five::five(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::five)
{
    ui->setupUi(this);
    this->setGeometry(QRect(200,100,850,800));
    QPushButton*jieshu=new QPushButton;
    jieshu->setParent(this);
    jieshu->setText("结\n束\n游\n戏");
    jieshu->move(800,600);
    jieshu->resize(50,200);
    connect(jieshu,&QPushButton::clicked,this,&five::close);
}

five::~five()
{
    delete ui;
}


//利用链表记录下棋步骤
class chessmemory
{
public:
    int x_memory;
    int y_memory;
    chessmemory*next=NULL;
};

chessmemory*chessmemoryl,*temp;
int times=0;

void five::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
//设置背景颜色
    //外墙
    painter.setPen(Qt::black);
    painter.setBrush(Qt::gray);
    painter.drawRect(0,0,850,800);
    //内墙
    painter.setPen(Qt::black);
    painter.setBrush(Qt::yellow);
    painter.drawRect(15,15,770,770);
    //棋盘
    painter.setPen(Qt::black);
    for(int i=1;i<14;i++)
    {
        painter.drawLine(15,15+i*55,785,15+i*55);
        painter.drawLine(15+i*55,15,15+i*55,785);
    }
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawEllipse(QPointF(400,400),5,5);
    painter.drawEllipse(QPointF(180,180),5,5);
    painter.drawEllipse(QPointF(180,620),5,5);
    painter.drawEllipse(QPointF(620,180),5,5);
    painter.drawEllipse(QPointF(620,620),5,5);
    painter.drawPixmap(15,15,770,770,QPixmap(":/qipan.jpg"));
    //画出棋子

    for(int i=0;i<15;i++)
    {
        for(int j=0;j<15;j++)
        {
            if(chess[i][j]==-1)
            {
                painter.setPen(Qt::black);
                painter.setBrush(Qt::black);
                painter.drawEllipse(QPointF(i*55+15,j*55+15),20,20);
            }
            if(chess[i][j]==1)
            {
                painter.setPen(Qt::white);
                painter.setBrush(Qt::white);
                painter.drawEllipse(QPointF(i*55+15,j*55+15),20,20);
            }
        }
    }
    if(blackwin(chess))
        {
            painter.setPen(Qt::white);
            painter.setBrush(Qt::white);
            painter.drawRect(350,350,200,100);
            painter.setPen(Qt::black);
            painter.drawText(400,400,QStringLiteral("black win"));
        }

        if(whitewin(chess))
        {
            painter.setPen(Qt::white);
            painter.setBrush(Qt::white);
            painter.drawRect(350,350,200,100);
            painter.setPen(Qt::black);
            painter.drawText(400,400,QStringLiteral("white win"));
        }

}


//确定棋子落点
int chessplace(int a)
{
    if(a>=15&&a<=35)return 5;
    if(a>=765&&a<=785)return 775;
    for(int start=70;start<=730;start+=55)
    {
        if(a>=start-20&&a<=start+20)return start-10;
    }
    return 0;
}


void five::mousePressEvent(QMouseEvent *event)
{
    int x=event->x();
    int y=event->y();
    if(x>=15&&x<785&&y>=15&&y<=785)
    {
        if(chessplace(x)!=0&&chessplace(y)!=0&&chess[(chessplace(x)-5/55)][(chessplace(y)-5)/55]==0)
        {
            chess_x=chessplace(x);
            chess_y=chessplace(y);
            int judge=0;
            for(int i=0;i<15;i++)
            {
                for(int j=0;j<15;j++)
                {
                    judge+=chess[i][j];
                }
            }
            if(judge==0)
            {
                //确定并记录棋子落点
                if(times==0)
                {
                    chessmemoryl=new chessmemory;
                    chessmemoryl->x_memory=(chess_x-5)/55;
                    chessmemoryl->y_memory=(chess_y-5)/55;//总觉得不太对。。。。。。。改成y的了
                    chess[(chess_x-5)/55][(chess_y-5)/55]=1;
                    temp=chessmemoryl;
                    times++;
                }
                else
                {
                    chessmemoryl=temp;
                    temp=new chessmemory;
                    temp->x_memory=(chess_x-5)/55;
                    temp->y_memory=(chess_y-5)/55;
                    temp->next=chessmemoryl;
                    //改这了,变换棋子颜色
                    chess[(chess_x-5)/55][(chess_y-5)/55]=1;

                }
            }
            else
            {
                chessmemoryl=temp;
                temp=new chessmemory;
                temp->x_memory=(chess_x-5)/55;
                temp->y_memory=(chess_y-5)/55;
                temp->next=chessmemoryl;
                chess[(chess_x-5)/55][(chess_y-5)/55]=-1;

            }
            update();//重新显示棋子图像
        }
        else
        {
            chess_x=-1;//避免出错/初始化-1
            chess_y=-1;
        }

    }
    else
    {
        chess_x=-1;
        chess_y=-1;
    }

}

bool five::blackwin(int (*a)[15])
{
    //黑棋横向五连胜利
    for(int i=0;i<15;i++)
    {
        for(int j=0;j<11;j++)
        {
            if(a[i][j]==-1&&a[i][j+1]==-1&&a[i][j+2]==-1&&a[i][j+3]==-1&&a[i][j+4]==-1)return true;
        }
    }
    //黑棋纵向五连成立
    for(int j=0;j<15;j++)
    {
        for(int i=0;i<11;i++)
        {
            if(a[i][j]==-1&&a[i+1][i]==-1&&a[i+2][j]==-1&&a[i+3][j]==-1&&a[i+4][j]==-1)return true;
        }
    }
    //黑棋左斜五连胜利
    for(int i=0;i<11;i++)
    {
        for(int j=0;j<11;j++)
        {
            if(a[i][j]==-1&&a[i+1][j+1]==-1&&a[i+2][j+2]==-1&&a[i+3][j+3]==-1&&a[i+4][j+4]==-1)return true;
        }
    }
    //黑子右斜五连胜利
    for(int i=14;i>=4;i--)
    {
        for(int j=0;j<11;j++)
        {
            if(a[i][j]==-1&&a[i-1][j+1]==-1&&a[i-2][j+2]==-1&&a[i-3][j+3]==-1&&a[i-4][j+4]==-1)return true;
        }
    }
    return false;
}
bool five::whitewin(int (*a)[15])
{
    //白棋横向五连胜利
    for(int i=0;i<15;i++)
    {
        for(int j=0;j<11;j++)
        {
            if(a[i][j]==1&&a[i][j+1]==1&&a[i][j+2]==1&&a[i][j+3]==1&&a[i][j+4]==1)return true;
        }
    }
    //白棋纵向五连成立
    for(int j=0;j<15;j++)
    {
        for(int i=0;i<11;i++)
        {
            if(a[i][j]==1&&a[i+1][i]==1&&a[i+2][j]==1&&a[i+3][j]==1&&a[i+4][j]==1)return true;
        }
    }
    //白棋左斜五连胜利
    for(int i=0;i<11;i++)
    {
        for(int j=0;j<11;j++)
        {
            if(a[i][j]==-1&&a[i+1][j+1]==-1&&a[i+2][j+2]==-1&&a[i+3][j+3]==-1&&a[i+4][j+4]==-1)return true;
        }
    }
    //白子右斜五连胜利
    for(int i=14;i>=4;i--)
    {
        for(int j=0;j<11;j++)
        {
            if(a[i][j]==1&&a[i-1][j+1]==1&&a[i-2][j+2]==1&&a[i-3][j+3]==1&&a[i-4][j+4]==1)return true;
        }
    }
    return false;
}

//悔棋函数
